package org.zhaozhen.basic.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zhaozhen.basic.businessexception.BusinessException;
import org.zhaozhen.basic.dto.LoginDto;
import org.zhaozhen.basic.service.ILoginService;
import org.zhaozhen.basic.util.AjaxResult;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        try {
            Map<String, Object> map = loginService.account(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {//自定义业务异常  接收业务层的异常
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        } catch (Exception e) {//系统级别的异常
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("噢哟,报错了,和我们没什么关系哈");
        }
    }

    /**
     * 微信扫码登录
     * @param parm
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String,String> parm){
        try {
            return loginService.wechat(parm);
        } catch (BusinessException e) {//自定义业务异常  接收业务层的异常
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        } catch (Exception e) {//系统级别的异常
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("噢哟,报错了,和我们没什么关系哈");
        }
    }

    /**
     * 微信绑定手机号接口
     * @param param
     * @return
     */
    @PostMapping("/binder/wechat")
    public AjaxResult binder(@RequestBody Map<String,String> param){
        try {
            Map<String, Object> map = loginService.binder(param);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {//自定义业务异常  接收业务层的异常
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        } catch (Exception e) {//系统级别的异常
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("噢哟,报错了,我们正在殴打程序员");
        }
    }

}
